package cn.ctyun.ipush.controller;

import cn.ctyun.ipush.constant.ResultInfo;
import cn.ctyun.ipush.dto.ResultDto;
import cn.ctyun.ipush.service.PictureService;
import cn.ctyun.ipush.utils.DataUtils;
import cn.ctyun.ipush.vo.PictureVo;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 16-7-11
 * Time: 下午8:24
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/picture")
public class PictureController {

    @Autowired
    private PictureService pictureService;

    /* 日志对象 */
    private final Logger LOG = LoggerFactory.getLogger(PictureController.class);

    /**
     *数据源保存，参数如下
     {
     "dataSourceName": "testdb",
     "dataSourceURL": "jdbc:oracle:thin:@localhost:1521:ORCL",
     "userName": "demo",
     "password": "123",
     "sqlListVo": {
     "sql1": "select * from pushtest1",
     "sql2": "select * from pushtest2"
     }
     }
     * @param request
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto save(HttpServletRequest request) {
        PictureVo pictureVo = null;
        ResultDto rd = new ResultDto();
        String requestJsonParam= DataUtils.getJsonFromRequest(request);

        if (StringUtils.isEmpty(requestJsonParam)) {
            return  rd.setInfo("params is empty").setStatus(ResultInfo.failure);//dataSoureVo = JSON.parseObject("{\"dataSourceName\":\"testDs\",\"dataSourceURL\":\"jdbc:oracle:thin:@localhost:1521:ORCL\",\"userName\":\"demo\",\"password\":\"123\",\"sqlListVo\":[{\"sql1\":\"select * from pushtest1\"},{\"sql2\":\"select * from pushtest2\"}],\"excelList\":[\"excelId1\",\"excelId2\"]}", DataSourceVo.class);
        } else {
            try{
                pictureVo = JSON.parseObject(requestJsonParam, PictureVo.class);
            }catch (Exception ex){
                LOG.info("param convert to object fail,check param format!!!");
                rd.setInfo("param convert to object fail,check param format!!!").setStatus(ResultInfo.failure);
                return rd;
            }
        }
        try {
            //图片名称不能为已经存在的
            if(!pictureService.isExist(pictureVo)){
                pictureService.save(pictureVo);
                rd.setStatus(ResultInfo.success).setInfo("save picture rule success");
            }else{
                rd.setStatus(ResultInfo.failure).setInfo("picture rule has existed");
            }
        } catch (Exception ex) {
            LOG.info("save picture rule failue :"+ex.getMessage());
            rd.setInfo("save picture rule failue").setStatus(ResultInfo.failure);
        }
        return rd;
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto update(HttpServletRequest request) {
        PictureVo pictureVo = null;
        ResultDto rd = null;
        String requestJsonParam= DataUtils.getJsonFromRequest(request);

        if (StringUtils.isEmpty(requestJsonParam)) {
            return rd.setInfo("param is empty").setStatus(ResultInfo.failure);
        } else {
            pictureVo = JSON.parseObject(requestJsonParam, PictureVo.class);
        }
        rd = new ResultDto();
        try {
            int count= pictureService.update(pictureVo);
            if(1==count){
                rd.setStatus(ResultInfo.success).setInfo("update picture rule succeess");
            }else{
                rd.setStatus(ResultInfo.failure).setInfo("update picture rule failue,return count not right");
            }
        } catch (Exception ex) {
            rd.setInfo("update  picture rule failue").setStatus(ResultInfo.failure);
        }
        return rd;
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto delete(HttpServletRequest request) {
        PictureVo pictureVo = null;
        ResultDto rd =  new ResultDto();
        String requestJsonParam= DataUtils.getJsonFromRequest(request);
        if (StringUtils.isEmpty(requestJsonParam)) {
            return  rd.setInfo("param is empty ,delete  picture rule  failue").setStatus(ResultInfo.failure);
        } else {
            pictureVo = JSON.parseObject(requestJsonParam, PictureVo.class);
        }
        try {
            int count= pictureService.delete(pictureVo.getPictureModelId());
            if(1==count){
                rd.setStatus(ResultInfo.success).setInfo(" delete picture rule success");
            }else{
                rd.setInfo("删除失败，存在引用该图片规则的任务，请先删除任务").setStatus(ResultInfo.failure);
            }
        } catch (Exception ex) {
            rd.setInfo("delete  picture rule  failue").setStatus(ResultInfo.failure);
        }
        return rd;
    }

    @RequestMapping(value = "queryListByPage", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto queryListByPage(HttpServletRequest request) {
        PictureVo pictureVo = null;
        ResultDto rd = null;
        String requestJsonParam= DataUtils.getJsonFromRequest(request);
        //    "dataSourceName": "testdb",
        //"curPage": 1,
        //        "itemsPerPage": 20
        if (StringUtils.isEmpty(requestJsonParam)) {
            return rd.setInfo("param is empty").setStatus(ResultInfo.failure);
        } else {
            pictureVo = JSON.parseObject(requestJsonParam, PictureVo.class);
        }
        rd = new ResultDto();
        try {
            List<PictureVo> pictureVoList= pictureService.queryListVoByPage(pictureVo);
            rd.setStatus(ResultInfo.success).setInfo(JSON.toJSONString(pictureVoList));
        } catch (Exception ex) {
            rd.setInfo("query mutliple picture failue").setStatus(ResultInfo.failure);
        }
        return rd;
    }
}
